<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author Cheng
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="false"
    :okAuth="'inspectionmanag:inTaskCollection:edit'"
    @register="registerDrawerDetail"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsInspectionmanagInTaskCollectionForm">
  import { ref, unref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { InTaskCollection, inTaskCollectionSave,inTaskCollectionListData } from '/@/api/inspectionmanag/inTaskCollection';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('inspectionmanag.inTaskCollection');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<InTaskCollection>({} as InTaskCollection);

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.isNewRecord ? t('详情') : t('详情'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('任务类型'),
      field: 'taskTypeName',
      component: 'Input',
      componentProps: {
        maxlength: 40,
        disabled: true,
      },
    },
    {
      label: t('任务状态'),
      field: 'taskStatus',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
      required: true,
    },
    {
      label: t('任务内容'),
      field: 'content',
      component: 'InputTextArea',
      colProps: { md: 24, lg: 24 },
      componentProps:{ disabled: true }
    },
    {
      label: t('任务要求'),
      field: 'remark',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('所属主体'),
      field: 'enterpriseName',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('领取时间'),
      field: 'receiveTime',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
        disabled: true
      },
    },
    {
      label: t('完成时间'),
      field: 'finishTime',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
        disabled: true
      },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { md: 24, lg: 12 },
  });

  const [registerDrawerDetail, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
  setDrawerProps({ loading: true });
  await resetFields();
  
  // 检查 data 中是否包含 tid
  if (!data ||!data.id) {
    console.error('未传入有效的 id');
    setDrawerProps({ loading: false });
    return;
  }

  const { id } = data;
  try {
    const res = await inTaskCollectionListData({ id });
    // 检查接口返回数据格式
    if (!res ||!res.list || res.list.length === 0) {
      console.error('接口返回数据格式错误，未找到有效的数据');
      setDrawerProps({ loading: false });
      return;
    }

    record.value = res.list[0] as InTaskCollection;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
  } catch (error) {
    console.error('调用 inTaskCollectionListData 接口出错:', error);
  } finally {
    setDrawerProps({ loading: false });
  }
});

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        tid: record.value.tid,
      };
      // console.log('submit', params, data, record);
      const res = await inTaskCollectionSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(error.message || t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
